<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="区域ID" prop="id" v-if="dataForm.id">
        <el-input v-model="dataForm.id" placeholder="区域ID" disabled></el-input>
    </el-form-item>
    <el-form-item label="上级区域ID" prop="parentId">
        <el-input v-model="dataForm.parentId" placeholder="上级区域ID"></el-input>
    </el-form-item>
    <el-form-item label="行政区域等级 1-省 2-市 3-区县 4-街道镇" prop="level">
        <el-input v-model="dataForm.level" placeholder="行政区域等级 1-省 2-市 3-区县 4-街道镇"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="完整名称" prop="wholeName">
        <el-input v-model="dataForm.wholeName" placeholder="完整名称"></el-input>
    </el-form-item>
    <el-form-item label="本区域经度" prop="lon">
        <el-input v-model="dataForm.lon" placeholder="本区域经度"></el-input>
    </el-form-item>
    <el-form-item label="本区域维度" prop="lat">
        <el-input v-model="dataForm.lat" placeholder="本区域维度"></el-input>
    </el-form-item>
    <el-form-item label="电话区号" prop="cityCode">
        <el-input v-model="dataForm.cityCode" placeholder="电话区号"></el-input>
    </el-form-item>
    <el-form-item label="邮政编码" prop="zipCode">
        <el-input v-model="dataForm.zipCode" placeholder="邮政编码"></el-input>
    </el-form-item>
    <el-form-item label="行政区划代码" prop="areaCode">
        <el-input v-model="dataForm.areaCode" placeholder="行政区划代码"></el-input>
    </el-form-item>
    <el-form-item label="名称全拼" prop="pinYin">
        <el-input v-model="dataForm.pinYin" placeholder="名称全拼"></el-input>
    </el-form-item>
    <el-form-item label="首字母简拼" prop="simplePy">
        <el-input v-model="dataForm.simplePy" placeholder="首字母简拼"></el-input>
    </el-form-item>
    <el-form-item label="区域名称拼音的第一个字母" prop="perPinYin">
        <el-input v-model="dataForm.perPinYin" placeholder="区域名称拼音的第一个字母"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy" v-if="dataForm.id">
        <el-input v-model="dataForm.createBy" placeholder="创建人" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updateBy" v-if="dataForm.id">
        <el-input v-model="dataForm.updateBy" placeholder="更新人" disabled></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
        <el-input v-model="dataForm.createTime" placeholder="创建时间" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime" v-if="dataForm.id">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间" disabled></el-input>
    </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/smart/oilregion'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: '',
          parentId: '',
          level: '',
          name: '',
          wholeName: '',
          lon: '',
          lat: '',
          cityCode: '',
          zipCode: '',
          areaCode: '',
          pinYin: '',
          simplePy: '',
          perPinYin: '',
          createBy: '',
          updateBy: '',
          createTime: '',
          updateTime: '',
        },
        dataRule: {
          parentId: [
            { required: true, message: '上级区域ID不能为空', trigger: 'blur' }
          ],

          level: [
            { required: true, message: '行政区域等级 1-省 2-市 3-区县 4-街道镇不能为空', trigger: 'blur' }
          ],

          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],

          wholeName: [
            { required: true, message: '完整名称不能为空', trigger: 'blur' }
          ],

          lon: [
            { required: true, message: '本区域经度不能为空', trigger: 'blur' }
          ],

          lat: [
            { required: true, message: '本区域维度不能为空', trigger: 'blur' }
          ],

          cityCode: [
            { required: true, message: '电话区号不能为空', trigger: 'blur' }
          ],

          zipCode: [
            { required: true, message: '邮政编码不能为空', trigger: 'blur' }
          ],

          areaCode: [
            { required: true, message: '行政区划代码不能为空', trigger: 'blur' }
          ],

          pinYin: [
            { required: true, message: '名称全拼不能为空', trigger: 'blur' }
          ],

          simplePy: [
            { required: true, message: '首字母简拼不能为空', trigger: 'blur' }
          ],

          perPinYin: [
            { required: true, message: '区域名称拼音的第一个字母不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (id) {
            getObj(id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
          this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
